import json
from typing import List, Dict, Any


def json_to_sentences(json_data: Dict[str, Any]) -> List[str]:
    """
    将包含因果事件记录的JSON数据转换为自然语言段落列表

    参数:
        json_data: 包含'causal_event_records'键的字典，值为事件记录列表

    返回:
        句子列表，每个句子对应一条记录的自然语言描述
    """
    sentences = []

    # 检查数据格式是否正确
    if 'causal_event_records' not in json_data:
        raise ValueError("JSON数据中缺少'causal_event_records'键")

    records = json_data['causal_event_records']

    for record in records:
        # 提取记录中的各个字段
        record_id = record.get('record_id', '未知ID')
        stock_code = record.get('stock_code', '未知股票')
        report_caption = record.get('report_caption', '无报告标题')
        event_caption = record.get('event_caption', '无事件描述')
        report_date = record.get('report_date', '未知日期').split('T')[0]  # 只取日期部分
        event_date = record.get('event_date', '未知日期').split('T')[0]
        strength = record.get('strength_of_association', 0)
        media_name = record.get('media_name', '未知媒体')
        strength_content = record.get('strength_content', '无详细说明')
        affect = record.get('affect', '中性')
        information_agent = record.get('information_agent', '未知主体')

        # 构建自然语言句子
        sentence = (f"对于股票{stock_code}，{media_name}报道了题为'{report_caption}'的事件。"
                    f"具体而言，{event_caption}。该事件舆情发生在{event_date}，"
                    f"相关公告于{report_date}发布，关联强度评分为{strength}分。"
                    f"事件影响为{affect}，涉及主体包括{information_agent}。"
                    f"详细说明：{strength_content}")

        sentences.append(sentence)

    return sentences


def parse_json_to_paragraphs(json_string: str) -> List[str]:
    """
    解析JSON字符串并转换为段落

    参数:
        json_string: JSON格式的字符串

    返回:
        自然语言段落列表
    """
    try:
        # 解析JSON字符串[1,8](@ref)
        data = json.loads(json_string)
        return json_to_sentences(data)
    except json.JSONDecodeError as e:
        raise ValueError(f"JSON解析错误: {e}")


# 使用示例
if __name__ == "__main__":
    # 你提供的JSON数据
    sample_json = """
    {
        "causal_event_records": [
            {
                "record_id": "45fabd46e5ae46f0a69021306ec90ddf",
                "stock_code": "603259.SZ",
                "report_caption": "出售所持药明合联5.08亿股股票",
                "event_caption": "药明康德减持药明合联引发市场对其'以子养母'策略的质疑",
                "report_date": "2025-03-31T16:00:00.000Z",
                "event_date": "2025-10-12T16:00:00.000Z",
                "strength_of_association": 8,
                "media_name": "财经媒体综合报道",
                "strength_content": "公司出售子公司股票的公告早于市场负面舆情近半年，表明市场初期未充分解读该行为的潜在影响，后期随着药明合联业绩高增长显现，投资者重新评估该交易，导致股价波动。",
                "affect": "负面",
                "information_agent": "药明康德, 药明合联"
            },
            {
                "record_id": "d9bf13f31d0547bab3ef2b49bb9f0a19",
                "stock_code": "603259.SZ",
                "report_caption": "发布2025年上半年业绩报告，营收208亿元，归母净利润85.6亿元",
                "event_caption": "药明康德业绩超预期引发媒体广泛正面报道",
                "report_date": "2025-07-27T16:00:00.000Z",
                "event_date": "2025-07-27T16:00:00.000Z",
                "strength_of_association": 9,
                "media_name": "财经媒体综合报道",
                "strength_content": "业绩公告与正面舆情几乎同时出现，媒体迅速解读并传播，显著增强市场信心，体现了高透明度信息披露对股价的积极促进作用。",
                "affect": "正面",
                "information_agent": "药明康德"
            }
        ]
    }
    """

    try:
        paragraphs = parse_json_to_paragraphs(sample_json)

        for i, paragraph in enumerate(paragraphs, 1):
            print(f"记录 {i}: {paragraph}\n")

    except ValueError as e:
        print(f"错误: {e}")